package com.jwinf.cdLibary.controller;

import com.jwinf.cdLibary.domain.Record;
import com.jwinf.cdLibary.domain.User;
import com.jwinf.cdLibary.service.CDService;
import com.jwinf.cdLibary.tool.WindowUtils;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

/**
 * @author jwinf
 * 归还图书窗体控制类
 */
public class ReturnController implements Initializable {
    @FXML
    private Label use;
    @FXML
    private Label backlbl;
    @FXML
    private TextField usetxt;
    @FXML
    private TableView table;
    @FXML
    private Button back;
    private User user;

    private ObservableList<Record> list;
    private CDService cdService = new CDService();

    // 数据初始化
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        componentInit();
        addListener();
    }

    // 设置用户
    public void setUser(User user){
        this.user = user;
    }

    // 数据初始化
    public void init(){
        usetxt.setText(user.getUsername());
        usetxt.setEditable(false);
        list = FXCollections.observableArrayList(cdService.find(user.getUsername()));
        table.setItems(list);
    }

    // 组件初始化
    private void componentInit(){
        // 改变标签的icon样式
        Image image;
        image = new Image(getClass().getResourceAsStream("/icon/currentUser.png"), 32, 32, false, false);
        use.setGraphic(new ImageView(image));
        image = new Image(getClass().getResourceAsStream("/icon/backIcon.png"), 32, 32, false, false);
        backlbl.setGraphic(new ImageView(image));

        // 给表格添加列 并绑定对应的属性
        TableColumn nameCol = new TableColumn("用户名");
        nameCol.setCellValueFactory(new PropertyValueFactory<>("username"));
        nameCol.setPrefWidth(120);
        TableColumn CdnameCol = new TableColumn("光盘名");
        CdnameCol.setCellValueFactory(new PropertyValueFactory<>("cdname"));
        CdnameCol.setPrefWidth(120);
        TableColumn stateCol = new TableColumn("状态");
        stateCol.setCellValueFactory(new PropertyValueFactory<>("action"));
        stateCol.setPrefWidth(120);
        TableColumn timeCol = new TableColumn("借阅时间");
        timeCol.setCellValueFactory(new PropertyValueFactory<>("time"));
        timeCol.setPrefWidth(140);

        // 给表格添加列
        table.getColumns().addAll(nameCol, CdnameCol, stateCol, timeCol);
    }

    // 添加监听器
    private void addListener(){
        // 进去组件时 改变鼠标样式
        WindowUtils.changeMouse(back);
        WindowUtils.changeMouse(backlbl);
    }

    // 返回用户主界面
    @FXML
    private void back() throws IOException {
        WindowUtils.showFrame("/com/jwinf/cdLibary/view/UserMainFrame.fxml", "/icon/loginIcon.png", "用户主界面", user);
        WindowUtils.destroyStage(backlbl);
    }

    // 归还图书
    @FXML
    private void returnCd(){
        // 获取选中的行
        int index = table.getSelectionModel().getSelectedIndex();
        if(index < 0){
            WindowUtils.showTip("请选择归还对象", "/icon/warnIcon.png", " -fx-background-color: linear-gradient(to top, #fff1eb 0%, #ace0f9 100%);");
            return;
        }
        // 获取选中的对象
        Record record = list.get(index);
        // 调用服务层的归还方法 并获得结果
        boolean isSuccess = cdService.back(record, user.getUsername());
        // 根据结果弹出弹窗提示用户
        if(isSuccess){
            WindowUtils.showTip("归还成功", "/icon/SuccessIcon.png", " -fx-background-color: linear-gradient(to top, #fff1eb 0%, #ace0f9 100%);");
            list = FXCollections.observableArrayList(cdService.find(user.getUsername()));
            table.setItems(list);
        }
        else {
            WindowUtils.showTip("归还失败", "/icon/warnIcon.png", " -fx-background-color: linear-gradient(to top, #fff1eb 0%, #ace0f9 100%);");
        }
    }

}
